<template name="home">
	<view>
hello


	</view>


</template>

<script>
	import dyDatePicker from "../../components/dy-Date/dy-Date.vue";
	import helper from '../../common/helper.js';
	import Bus from '../../common/bus.vue';

	// function getDate(type) {
	// 	const date = new Date();

	// 	let year = date.getFullYear();
	// 	let month = date.getMonth() + 1;
	// 	let day = date.getDate();

	// 	if (type === 'start') {
	// 		day = day - 10;
	// 	} else if (type === 'end') {
	// 		day = day + 10;
	// 	}

	// 	return `${year}-${month}-${day}`;
	// }
	function getDate(type) {
		const date = new Date();

		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();

		if (type === 'start') {
			year = year - 2;
		} else if (type === 'end') {
			year = year + 2;
		}
		// month = month > 9 ? month : '0' + month;;
		// day = day > 9 ? day : '0' + day;

		return `${year}-${month}-${day}`;
	}
	export default {
		name: "home",
		components: {
			dyDatePicker
		},
		data() {
			
			return {
				startDate: getDate('start'),
				endDate: getDate('end'),
				hasUserInfo: false,
				userInfo: {},
				homeInfo: {},
				showType: '',
				// dateDay: '',
				dateDay: getDate('current'),
				modalName: '',
				
			};
		},
		created() {
			console.group('created 组件创建完毕状态===============》');
			let that = this;
			//查看是否授权
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					console.log(res);
					if (res.errMsg === 'getStorage:ok') {
						that.userInfo = res.data;
						that.hasUserInfo = true;
						that.getHomeInfo();
					} else {
						that.hasUserInfo = false;
					}
				},
				fail: function(e) {
					console.log(e);
				}
			});

			Bus.$on('userInfo', target => {
				console.log('home bus');
				let that = this;
				//查看是否授权
				uni.getStorage({
					key: 'userInfo',
					success: function(res) {
						console.log(res);
						if (res.errMsg === 'getStorage:ok') {
							that.userInfo = res.data;
							that.hasUserInfo = true;
							that.getHomeInfo();
						} else {
							that.hasUserInfo = false;
						}
					},
					fail: function(e) {
						console.log(e);
					}
				});
			});

		},
		beforeDestroy() {
			console.log('home beforeDestroy');
			Bus.$off('userInfo');
		},

		methods: {
			setUserInfo(){
				
			},
			// showModal(e) {
			// 	this.modalName = 'bottomModal'
			// },
			// hideModal(e) {
			// 	this.modalName = ''
			// },

			// bindChange (e) {
   //              const val = e.detail.value
   //              this.year = this.years[val[0]]
   //              this.month = this.months[val[1]]
   //              this.day = this.days[val[2]]
   //          },

			bindDateChange: function(e) {
				this.dateDay = e.target.value;
				console.log(this.dateDay);

				// showType: 1 用户没有记录 , 2:存在未结束记录，计算来了多长时间, 3:记录都已结束，预测下次时间
				if (this.showType === 1) {
					this.ymComes();
				} else if (this.showType === 2) {
					this.ymGoes();
				} else if (this.showType === 3) {
					this.ymComes();
				} else {
					uni.showToast({
						duration: 2000,
						title: "未知类型",
						icon: 'none'
					})
				}

			},

			setHomeData() {

			},

			/**
			 * 获取首页信息
			 */
			getHomeInfo() {

				let that = this;
				let playod = {
					userId: that.userInfo.userId,
				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appYmrecord/open/homeInfo', //仅为示例，并非真实接口地址。
					method: 'get',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},
					complete() {
						uni.hideLoading();
					},
					success: (res) => {

						if (res.data.code === 0) {
							that.homeInfo = res.data.data;
							that.showType = res.data.data.showType;

							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
							return;
						}
					},
					fail: (err) => {
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});
			},

			/**
			 * 来了
			 */
			ymComes() {

				let that = this;
				let playod = {
					userId: that.userInfo.userId,
					beginDay: that.dateDay,
				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appYmrecord/open/ymComes', //仅为示例，并非真实接口地址。
					method: 'get',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},
					complete() {
						uni.hideLoading();
					},
					success: (res) => {

						if (res.data.code === 0) {
							uni.showToast({
								duration: 2000,
								title: "请求成功"
							});
							setTimeout(function() {

								that.getHomeInfo();

							}, 1000);


							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
							return;
						}
					},
					fail: (err) => {
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});
			},

			/**
			 * 走了
			 */
			ymGoes() {

				let that = this;
				let playod = {
					userId: that.userInfo.userId,
					dateDay: that.dateDay,
				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appYmrecord/open/ymGoes', //仅为示例，并非真实接口地址。
					method: 'get',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},
					complete() {
						uni.hideLoading();
					},
					success: (res) => {

						if (res.data.code === 0) {
							uni.showToast({
								duration: 2000,
								title: "请求成功"
							});
							setTimeout(function() {

								that.getHomeInfo();

							}, 1000);

							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
							return;
						}
					},
					fail: (err) => {
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});
			}


		},


	}
</script>

<style>
	.page {
		height: 100vh;
	}

	/* 	page {
		padding-top: 45px;
	} */


	.home-bg {
		display: flex;
		flex-direction: column;
		justify-content: center;
		font-size: 45rpx;
		color: rgb(240, 98, 146);
		height: 100%;

	}

	.hm-empty {
		/* 用于将按钮撑到底部，这个效果只有在父布局 height: 100%; 下才可以*/
		flex: 1;
	}

	.hm-num {
		font-size: 90rpx;
		margin: 40rpx;
	}

	.hm-btn {
		margin-bottom: 300rpx;
	}
</style>
